//
//  DMCell.m
//  DM
//
//  Created by XingShuai on 2020/9/23.
//  Copyright © 2020 dashuai. All rights reserved.
//

#import "DMCell.h"

@implementation DMCell

- (CGFloat)setupSubviews {
    self.backgroundColor = [UIColor colorWithRed:0.1 green:0 blue:0 alpha:0.25];
    
    NSDictionary *body = self.message.body;
    
    return [self makeType1Cell:body];
}

// 特殊弹幕样式
- (CGFloat)makeType1Cell:(NSDictionary *)body; {
    UIImageView *head = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 20, 20)];
    head.layer.cornerRadius = 20 * .5;
    head.clipsToBounds = YES;
    head.contentMode = UIViewContentModeScaleAspectFill;
    [self setImageWithImagePath:body[@"head"] forImageView:head];
    [self addSubview:head];
    
    UILabel *label2 = [[UILabel alloc] init];
    label2.textColor = body[@"color"];
    label2.font = [UIFont systemFontOfSize:12];
    label2.text = body[@"content"];
    [self addSubview:label2];
    
    // 计算宽度
    [label2 sizeToFit];
    
    
    label2.frame = ({
        CGRect frame = label2.frame;
        frame.origin.x = CGRectGetMaxX(head.frame) + 5.f;
        frame.origin.y = 0;
        frame.size.height = self.frame.size.height;
        frame;
    });
     
    return MAX(CGRectGetMaxX(label2.frame), CGRectGetMaxX(label2.frame));
}

- (void)setImageWithImagePath:(NSString *)urlString forImageView:(UIImageView *)imageView; {
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]];
        dispatch_async(dispatch_get_main_queue(), ^{
            imageView.image = image;
        });
    });
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
